package com.felton.springboot.shrio.config;

import com.felton.springboot.shrio.entity.Permission;
import com.felton.springboot.shrio.entity.Role;
import com.felton.springboot.shrio.entity.User;
import com.felton.springboot.shrio.service.PermissionService;
import com.felton.springboot.shrio.service.RoleService;
import com.felton.springboot.shrio.service.UserService;
import jdk.nashorn.internal.parser.Token;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 类  名：com.felton.springboot.shrio.config.MyShrioRealm
 * 类描述：todo
 * 创建人：liurui
 * 创建时间：2020/6/10 11:23
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author liurui
 * @version 1.0
 */
public class MyShiroRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleservice;

    @Autowired
    private PermissionService permissionService;


    /**
     * 权限信息
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo  authorizationInfo = new SimpleAuthorizationInfo();
        User user = (User) principalCollection.getPrimaryPrincipal();

        List<Role> roleList = roleservice.getRoleListByUserName(user.getUserName());
        List<Permission> permissionList = null;
        if (roleList.size() > 0) {
            //添加角色
            for (Role role : roleList) {
                authorizationInfo.addRole(role.getRole());
                permissionList = permissionService.getPermissionListByRoleId(role.getRoleId());
                for (Permission permission : permissionList) {
                    //添加权限
                    authorizationInfo.addStringPermission(permission.getPermission());
                }
            }
        }

        return authorizationInfo;
    }

    /**
     * 身份认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String userName = (String) authenticationToken.getPrincipal();
        User user = userService.findByUserName(userName);
        if (user == null) {
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                user,
                user.getPassword(),
                getName()
        );
        return authenticationInfo;
    }


}
